<script setup>

import { storeToRefs } from 'pinia';
import dayjs from 'dayjs';

useHead({
	title: 'HLS: Отчет старших техников',
})

// const doc_finish_nar = ref({});
const selectPeriodID = ref(0)

const user = useCookie('user');
if (user.value == null) {
	navigateTo('/login');
}
const mpp = useCookie('mpp');
const urlHome = useUrl();
const route = useRouter();
const period_disable = ref(false)
const isEnableReport = ref(false)
const isResetPage = ref(false)
const podrSelect = ref(0)
const pend = ref(false)
const doc_pdr_list = ref([])
const year = ref(0)
const month = ref(0)
const lgot = ref(0)
const dn = ref(new Date())
const dk = ref(new Date())
const selectProfile = ref(2)

const selectLgotList = ref([{
	label: 'Тип льготы',
	value: 0,
	disabled: true
}, {
	label: 'Льготные наряды',
	value: 'lgot'
}, {
	label: 'Платные наряды',
	value: 'pd'
}
])

// const { data: periods } = await useFetch(`${urlHome.value}lab/period_date/`)

const { data: periods } = await useFetch(`${urlHome.value}lab/period_date_dj/`)

const { data: podrList } = await useFetch(`${urlHome.value}lab/mpp_podr_list/`, {
	query: {
		mpp: mpp.value
	}
}
)

const submitButton = computed(() => {
	return lgot.value !== 0 && podrSelect.value !== 0 && selectPeriodID.value !== 0
})


const profile = [{
	label: 'Ортопедия',
	value: 2,
}, {
	label: "Ортодонтия",
	value: 3
}]

const selectPeriod = computed(() => periods.value.find((element) => element.id == selectPeriodID.value))

async function get_nar_usl() {
	pend.value = true;
	let formData = new FormData();
	if (lgot.value == 'lgot') {
		formData.append('dn', dayjs(selectPeriod.value.dn_lg).format('YYYY-MM-DD'))
		formData.append('dk', dayjs(selectPeriod.value.dk_lg).format('YYYY-MM-DD'))
	} else if (lgot.value == 'pd') {
		formData.append('dn', dayjs(selectPeriod.value.dn_pl).format('YYYY-MM-DD'))
		formData.append('dk', dayjs(selectPeriod.value.dk_pl).format('YYYY-MM-DD'))
	}
	formData.append('year', selectPeriod.value.years)
	formData.append('month', selectPeriod.value.months)
	formData.append('lgot', lgot.value)
	formData.append('podr', podrSelect.value)
	formData.append('profile', selectProfile.value)
	console.log("profile: ", selectProfile.value)
	isResetPage.value = true
	const response = await $fetch(`${urlHome.value}lab/get_stat_for_zav2/`, {
		method: 'POST',
		body: formData
	})
	if (response.ok) {
		console.log(response)
		doc_pdr_list.value = response.doc_list
		year.value = response.year
		month.value = response.month
		lgot.value = response.lgot
		dn.value = response.dn
		dk.value = response.dk
	} else {

		alert("Ошибка загрузки отчета")
	}
	pend.value = false
}

function change_lgot() {
	period_disable.value = true
}

function clear_report() {
	location.reload();
}

const { data: specList } = await useFetch(`${urlHome.value}lab/list_all_prof`)

// async function get_brigades() {
// 	const { data: brigade } = await useFetch(urlHome.value + 'naryad/get_list_spec/')

// 	if (brigade.value.status === 'ok') {

// 		setBrigade(brigade.value.brigade)
// 	}
// 	else {
// 		return
// 	}
// }

// const { data: specList } = await useFetch(`${urlHome.value}lab/list_all_prof`)

// await get_brigades();


const refresh = () => refreshNuxtData()

</script>
<style>
.list-complete-move,
/* apply transition to moving elements */
.list-complete-enter-active,
.list-complete-leave-active {
	transition: all 0.5s ease;
}

.list-complete-item {
	transition: all 0.8s ease;
	display: inline-block;
	margin-right: 10px;
}

.list-complete-enter-from,
.list-complete-leave-to {
	opacity: 0;
	transform: translateY(30px);
}

.list-complete-leave-active {
	position: absolute;
}
</style>

<template>
	<div>
		<div class="m-4">
			<div v-if="pend">
				<LoadFetchAnimation></LoadFetchAnimation>
			</div>
			<!-- <div class="font-bold">Итого сумма: {{ Number((labSum + uslSum).toFixed(2)).toLocaleString() }} руб.
			</div>
			<div class="font-bold">В том числе с лабораторией: {{ Number(labSum.toFixed(2)).toLocaleString() }} руб.
			</div>
			<div class="font-bold">В том числе без лаборатории: {{ Number(uslSum.toFixed(2)).toLocaleString() }} руб.
			</div> -->
			<div class="mb-2 text-lg space-x-1 font-semibold text-gray-900 dark:text-white flex">
				<div class="mr-2">Период:</div>
				<USelect v-model="lgot" :options="selectLgotList" @change="change_lgot" color="green" />
				<USelect v-model.number="podrSelect" :options="podrList" option-attribute="lpu_name"
					value-attribute="lpu_number" color="green" />
				<USelect v-model="selectPeriodID" :disabled="!period_disable" :options="periods"
					option-attribute="nmonth" value-attribute="id" color="green" />
				<USelect v-model="selectProfile" :options="profile" option-attribute="label" value-attribute="value"
					color="green" />
				<div v-if="isResetPage">
					<UButton class="btn btn-sm btn-primary" @click="clear_report" label="Очистить" />
				</div>
				<div v-else>
					<UButton label="Сформировать" class="btn btn-sm btn-primary" :disabled="!submitButton"
						@click="get_nar_usl" />
				</div>
			</div>
			<div>*Отклоненные наряды будут возвращены врачу как открытые наряды</div>
			<div>Таким образом отклоненный наряд врач сможет снова подать в отчет на проверку.</div>
			<StDoc v-for="({ mpp, fio, lab_pdzr_tech, lab_dzr, usl_pdzr_tech, usl_dzr }) in doc_pdr_list" :key="mpp"
				:mpp="mpp" :fio="fio" :lab_pdzr_tech="lab_pdzr_tech" :lab_dzr="lab_dzr" :usl_pdzr_tech="usl_pdzr_tech"
				:usl_dzr="usl_dzr" :dn="dn" :dk="dk" :lgot="lgot" :profile="selectProfile" :year="year" :month="month"
				:specList="specList" :get_nar_usl="get_nar_usl">
			</StDoc>
		</div>
	</div>
</template>